﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Windows.Forms;

namespace MainApp.Helper
{
    public class AppHelper
    {
        private static string imageRootPath = "";
        private static string productListTemplet = null;
        private static string productDetailsTemplet = null;
        private static string entityListTemplet = null;
        private static string entityDetailsTemplet = null;
        private static int thumbnailWidth = 160;
        private static int thumbnailHeight = 90;
        private static int pageSize = 3;

        static AppHelper()
        {

        }

        public static bool Init()
        {
            try
            {
                string path = ConfigurationManager.AppSettings["ImageRootPath"];
                if (string.IsNullOrEmpty(path))
                {
                    path = AppDomain.CurrentDomain.BaseDirectory + "Images\\";
                }

                if (path.Contains("{AppDir}"))
                {
                    path = path.Replace("{AppDir}", AppDomain.CurrentDomain.BaseDirectory);
                }
                imageRootPath = path;

                if (!Directory.Exists(imageRootPath))
                {
                    Directory.CreateDirectory(imageRootPath);
                }

                using (TextReader reader = File.OpenText(AppDomain.CurrentDomain.BaseDirectory + "Templet\\ProductList.htm"))
                {
                    productListTemplet = reader.ReadToEnd();
                }

                using (TextReader reader = File.OpenText(AppDomain.CurrentDomain.BaseDirectory + "Templet\\ProductDetails.htm"))
                {
                    productDetailsTemplet = reader.ReadToEnd();
                }

                using (TextReader reader = File.OpenText(AppDomain.CurrentDomain.BaseDirectory + "Templet\\EntityList.htm"))
                {
                    entityListTemplet = reader.ReadToEnd();
                }

                using (TextReader reader = File.OpenText(AppDomain.CurrentDomain.BaseDirectory + "Templet\\EntityDetails.htm"))
                {
                    entityDetailsTemplet = reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("初始化错误：" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

        public static string ImageRootPath
        {
            get { return imageRootPath; }
        }

        public static int ThumbnailWidth { get { return thumbnailWidth; } }

        public static int ThumbnailHeight { get { return thumbnailHeight; } }

        public static int PageSize
        {
            get { return AppHelper.pageSize; }
        }

        public static string ProductListTemplet
        {
            get { return productListTemplet; }
        }

        public static string ProductDetailsTemplet
        {
            get { return productDetailsTemplet; }
        }

        public static string EntityListTemplet
        {
            get { return entityListTemplet; }
        }

        public static string EntityDetailsTemplet
        {
            get { return entityDetailsTemplet; }
        }

        public static readonly string P_COUNT_TAG = "{#ProductCount#}";
        public static readonly string USED_TIME_TAG = "{#UsedTime#}";
        public static readonly string P_PANEL_TAG = "{#ProductPanel#}";
        public static readonly string P_PAGE_TAG = "{#ProductPage#}";
        public static readonly string P_NAME_TAG = "{#Name#}";
        public static readonly string P_PRICE_TAG = "{#Price#}";
        public static readonly string P_SUMMARY_TAG = "{#Summary#}";
        public static readonly string P_DETAILS_TAG = "{#Details#}";
        public static readonly string P_ID_TAG = "{#ID#}";
        public static readonly string P_IDX_IMG_TAG = "{#IndexImage#}";
        public static readonly string E_COUNT_TAG = "{#EntityCount#}";
        public static readonly string E_PANEL_TAG = "{#EntityPanel#}";
        public static readonly string E_PAGE_TAG = "{#EntityPage#}";
        public static readonly string E_TITLE_TAG = "{#Title#}";
        public static readonly string E_CONTENT_TAG = "{#Content#}";
        public static readonly string E_ID_TAG = "{#ID#}";
    }
}
